import NIM from '@/sdk/NIM_Web_NIM_v8.2.0.js'
import Netcall from '@/sdk/NIM_Web_Netcall_v8.2.0.js'

NIM.use(Netcall)
export default {
    data() {
        return {
            nim: '',
            callTimer: null,
            beCalledInfo: {},

        }
    },
    computed: {
        channelName() {
            return this.beCalledInfo && this.beCalledInfo.channelName ? this.beCalledInfo.channelName : this.userInfo.account + 'channel'
        }
    },
    methods: {
        onConnect() {
            console.log('连接成功');
        },
        onWillReconnect(obj) {
            // 此时说明 SDK 已经断开连接, 请开发者在界面上提示用户连接已断开, 而且正在重新建立连接
            console.log('即将重连');
            this.$message.success('即将重连')
            console.log(obj.retryCount);
            console.log(obj.duration);
        },
        onDisconnect(error) {
            // 此时说明 SDK 处于断开状态, 开发者此时应该根据错误码提示相应的错误信息, 并且跳转到登录页面
            console.log('丢失连接');
            console.log(error);
            if (error) {
                switch (error.code) {
                    // 账号或者密码错误, 请跳转到登录页面并提示错误
                    case 302:
                        this.$message.warning('账号或者密码错误')
                        this.$router.push('login')
                        break;
                    // 重复登录, 已经在其它端登录了, 请跳转到登录页面并提示错误
                    case 417:
                        this.$message.warning('重复登录, 已经在其它端登录')
                        this.$router.push('login')
                        break;
                    // 被踢, 请提示错误后跳转到登录页面
                    case 'kicked':
                        this.$message.warning('不允许同一个帐号在多个地方同时登录')
                        this.$router.push('login')
                        break;
                    default:
                        break;
                }
            }
        },
        // 挂断
        hangup() {
            this.showAnswer = false
            const params = {
                form: this.userInfo.form,
                type: 'hangup',
            }
            this.callIng(params, this.beCalledInfo.form)
            this.handleOver()
        },
        videoCall() {
            this.video = true
            this.showCall = false
            const params = {
                form: this.userInfo.account,
                channelName: this.channelName,
                type: 'callVideo',
            }
            if (this.checkedUser.length) {
                this.checkedUser.map(item => {
                    this.callIng(params, item.account)
                })
                if(!this.localStream) this.init()
            } else {
                this.$message.warning('未选中通话人员')
            }
        },
        radioCall() {
            this.video = false
            this.showCall = false
            const params = {
                form: this.userInfo.account,
                channelName: this.channelName,
                type: 'callRadio',
            }
            if (this.checkedUser.length) {
                this.checkedUser.map(item => {
                    this.callIng(params, item.account)
                })
                if(!this.localStream) this.init()
            } else {
                this.$message.warning('未选中通话人员')
            }
        },
        beCall() {
            this.showAnswer = false
            const params = {
                form: this.userInfo.account,
                type: 'callSuccess',
            }
            this.callIng(params, this.beCalledInfo.form)
            if(!this.localStream) this.init()
        },
        callIng(content, to) {
            // callVideo 视频通话 callRadio 语音通话
            this.nim.sendCustomMsg({
                scene: 'p2p',
                to,
                content: JSON.stringify(content),
                done: (err, msg) => {
                    console.log(err, msg)
                }
            })
            if (content.type !== 'hangup') {
                // this.callTimer = setTimeout(function () {
                //     console.log('超时未接听, hangup');
                // }, 1000 * 30);
            } else if (content.type !== 'callVideo') {
            }

        },
        onError(error) {
            console.log(error);
        },
        initNetcall() {
            // 46ad6f167c00955a91fcaf1cdf322fe1 demo11 
            // demo22  cb55c71a0f49df80f1889e40ffc44b66
            const nim = this.nim = NIM.getInstance({
                appKey: this.config.appkey,
                token: this.userInfo.token,
                account: this.userInfo.account,
                onconnect: (obj) => {
                },
                onwillreconnect: this.onWillReconnect,
                ondisconnect: this.onDisconnect,
                onerror: this.onError,
                db: false,
                onmsg: (msg) => {
                    const content = JSON.parse(msg.content)
                    if (!this.remoteStreams.length) {
                        if (content.type === 'callVideo') {
                            // 来视频电话了  显示ui
                            this.video = true
                        } else if (content.type === 'callRadio') {
                            // 来语音通话了
                            this.video = false
                        }
                        this.showAnswer = true
                        this.beCalledInfo = content
                    }
                    if (content.type === 'hangup') {
                        this.beCalledInfo = {}
                        this.$message.warning(`${content.form}挂断了`)
                    }
                }
            })

        }
    },
    mounted() {
        this.initNetcall()

    }
}